import os
import sys
from config import settings
import json


def taoba_pay(username=None, cost=None):
    '''
    支付接口
    '''
    user_atm = os.path.join(settings.DB_DIR, username + '_atm.json')
    if os.path.exists(user_atm):
        if cost:
            with open(user_atm, 'r') as f:
                user_atm_info = json.load(f)
            user_atm_info.get(username)['balance'] -= cost
            with open(user_atm, 'w') as f:
                json.dump(user_atm_info, f)
            return json.dumps({'paycode': 0})
        else:
            return json.dumps({'paycode': 1})
    else:
        return json.dumps({'paycode': 2})


def main():
    '''
    ATM 入口
    '''
    print(' Welcome Taoba ATM '.center(50, '='))
